package com.example.task1.ui.AllService.traffic;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.TrafficCartListBean;
import com.example.task1.http.Api;
import com.example.task1.ui.BaseAdapter;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

public class TrafficCartListAdapter extends BaseAdapter<TrafficCartListBean.RowsDTO> {

    private Context context;
    private List<TrafficCartListBean.RowsDTO> mData = new ArrayList<>();
    private TextView tvPlateNo;
    private TextView tvEngineNo;
    private TextView tvType;
    private Button btnRemove;
    private int log;

    public TrafficCartListAdapter(Context context,int log) {
        this.context = context;
        this.log = log;
    }

    @Override
    protected void setData(List<TrafficCartListBean.RowsDTO> rows) {
        mData.clear();
        mData.addAll(rows);
        notifyDataSetChanged();
    }

    @Override
    protected int getLayout() {
        return R.layout.trafficcartlist;
    }

    @Override
    protected Context getContext() {
        return null;
    }

    @Override
    protected List<TrafficCartListBean.RowsDTO> getData() {
        return mData;
    }

    @Override
    protected void onBind(myHolder holder, TrafficCartListBean.RowsDTO date) {

    }

    @Override
    protected void onBind(myHolder holder, TrafficCartListBean.RowsDTO date, int position) {
        tvEngineNo.setText(date.getEngineNo());
        tvPlateNo.setText(date.getPlateNo());
        tvType.setText(date.getType());
        if (log !=0) {
            btnRemove.setVisibility(View.GONE);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (log != 0) {
                    MyApplication.Traffic.cartDate = date;
                    ((TrafficCartActivity)context).finish();
                }else {
                    MyApplication.Traffic.cartDate = date;
                    MyApplication.intent(context,TrafficModificationCartActivity.class);
                }
            }
        });
        btnRemove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Call<ApiResp> del = Api.api().del(MyApplication.TOKEN, "/prod-api/api/traffic/car/" + date.getId());
                Api.send(del, ApiResp.class, new CallBack<ApiResp>() {
                    @Override
                    public void success(ApiResp data) {
                        MyApplication.show("解绑成功");
                        mData.remove(position);
                        notifyItemRemoved(position);
                    }

                    @Override
                    public void error(String msg) {

                    }
                });
            }
        });
    }

    @Override
    protected void holder(View itemView) {
        initView(itemView);
    }

    private void initView(View view) {
        tvPlateNo = (TextView) view.findViewById(R.id.tv_plateNo);
        tvEngineNo = (TextView) view.findViewById(R.id.tv_engineNo);
        tvType = (TextView) view.findViewById(R.id.tv_type);
        btnRemove = (Button) view.findViewById(R.id.btn_remove);
    }
}
